package com.zcm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.hibernate.sql.Alias;

import com.zcm.entity.address;
import com.zcm.entity.commodity;
import com.zcm.entity.shopping_cart;

import com.zcm.entity.user;

public class CommodityDao extends BaceDao {
	/**
	 * @author Xrh 模糊搜索的方法，使用hql自定义语句
	 */
	public List<commodity> SelectCommodity(String SelectText) {
		List<commodity> list = new ArrayList<>();
		String hql = "from commodity c where c.com_name  like ? or c.category like ? ";
		list = session.createQuery(hql).setString(0, "%" + SelectText + "%").setString(1, "%" + SelectText + "%")
				.list();
		tran.commit();
		session.close();
		factory.close();
		return list;
	}



	/**
	 * @zzx 删除购物车
	 *
	 */
	public int DeleteShopping_cart(int comid,int userid) {
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("delete from shopping_cart where comid=? and userid=?");
			pstm.setInt(1, comid);
			pstm.setInt(2, userid);
			int rs = pstm.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return 0;
	}

	


	/**
	 * @author Xrh 进入主页时查询所有商品信息，用EL表达式放入jsp内
	 * 
	 */
	public List<commodity> SelectAllcommodity() {
		String hql = "from commodity";
		// 自定义hql语句，返回List判断Size
		List<commodity> list = session.createQuery(hql).list();

		tran.commit();
		session.close();
		factory.close();
		return list;
	}

	/**
	 * @author Xrh 进入购物车时查询所有自己的购物车信息
	 * 
	 */
	public List<commodity> SelectUserShoppingCart(Integer userid) {
		try {
			List<commodity> list = new ArrayList<>();
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("select * from shopping_cart where userid=?");
			PreparedStatement pstmc = con.prepareStatement("select * from commodity where comid=?");
			pstm.setInt(1, userid);
			ResultSet rs = pstm.executeQuery();
			commodity c = null;
			while (rs.next()) {
				pstmc.setInt(1, rs.getInt("comid"));
				ResultSet rs1 = pstmc.executeQuery();
				while (rs1.next()) {
					c = new commodity();
					c.setComid(rs1.getInt("comid"));
					c.setCategory(rs1.getString("category"));
					c.setCom_name(rs1.getString("com_name"));
					c.setImage(rs1.getString("image"));
					c.setPrice(rs1.getDouble("price"));
					list.add(c);
				}
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @author Xrh 根据商品id查询商品详情
	 * 
	 */
	public commodity SelectCommodityByComid(int comid) {

		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("select * from commodity where comid=?");
			pstm.setInt(1, comid);
			ResultSet rs = pstm.executeQuery();
			commodity c = null;
			while (rs.next()) {
				c = new commodity();
				c.setComid(rs.getInt("comid"));
				c.setCategory(rs.getString("category"));
				c.setCom_name(rs.getString("com_name"));
				c.setImage(rs.getString("image"));
				c.setPrice(rs.getDouble("price"));
			}
			return c;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * @author Xrh
	 * @return 添加购物车信息
	 */
	public boolean AddShopping_Cart(shopping_cart sc) {
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("insert into shopping_cart(userid,comid,num) values(?,?,?)");
			pstm.setInt(1, sc.getUserid());
			pstm.setInt(2, sc.getComid());
			pstm.setInt(3, sc.getNum());
			int rs = pstm.executeUpdate();
			if (rs > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * @author Xrh 执行添加时判断购物车内是否有相关商品 有的返回i=1
	 */
	public int shopping_cartIsExist(int comid, int userid) {
		int i = 0;
		try {
			Connection con = BaceDaoForJDBC.getCon();
			PreparedStatement pstm = con.prepareStatement("select comid from shopping_cart where comid =? and userid = ? ");
			pstm.setInt(1, comid);
			pstm.setInt(2, userid);
			ResultSet rs = pstm.executeQuery();
			while(rs.next()){
				i = 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}
}
